package com.incross.dawin.http.parser;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import android.util.Log;

public class TrackingInfoParser {
	private String mStartTag = "", mQuarterTag = "", mHalfTag = "", mQuarter3Tag = "", mEndTag = "", mSkipTag = "", mChargeTag = "";
	
	private int mQuarterTime = -1, mHalfTime = -1, mQuarter3Time = -1, mChargeTime = -1;
	
	// 20141216 파싱시 발생한 문제 체크용 boolean값 추가
	private boolean mIsParsingProblemOccured = false;
	
	public TrackingInfoParser(NodeList list) {
		if(list != null) {
			try {
				NodeList child = list.item(0).getChildNodes();
				
				for(int i = 0; i < child.getLength(); i++) {
					if(child.item(i).getNodeName().equals("Tracking")) {
						NamedNodeMap attributes = child.item(i).getAttributes();
						String value = child.item(i).getFirstChild().getNodeValue();
						
						AttributeObject attr = new AttributeObject();
						for(int j = 0; j < attributes.getLength(); j++) {
							String nodeValue = attributes.item(j).getNodeValue();
							String nodeName = attributes.item(j).getNodeName();
							if(nodeName.equals("rt")) {
								attr.setRtValue(nodeValue);
							} else if(nodeName.equals("event")) {
								attr.setEventValue(nodeValue);
							}
						}
						
						
						
						if(attr.getEventValue().equals("start")) {
							mStartTag = value;
						} else if(attr.getEventValue().equals("skip")) {
							mSkipTag = value;
						} else if(attr.getEventValue().equals("complete")) {
							mEndTag = value;
						} else if(attr.getEventValue().equals("firstQuartile")) {
							mQuarterTag = value;
							mQuarterTime = Integer.parseInt(attr.getRtValue()) * 1000;
						} else if(attr.getEventValue().equals("midpoint")) {
							mHalfTag = value;
							mHalfTime = Integer.parseInt(attr.getRtValue()) * 1000;
						} else if(attr.getEventValue().equals("thirdQuartile")) {
							mQuarter3Tag = value;
							mQuarter3Time = Integer.parseInt(attr.getRtValue()) * 1000;
						} else if(attr.getEventValue().equals("paytime")) {
							mChargeTag = value;
							mChargeTime = Integer.parseInt(attr.getRtValue()) * 1000;
						}
						
						
					}
				}
				
				
				/*BKLog.w"start tag : " + mStartTag);
				BKLog.w"1quarter tag : " + mQuarterTag + ", rt = " + mQuarterTime);
				BKLog.w"2quarter tag : " + mHalfTag + ", rt = " + mHalfTime);
				BKLog.w"3quarter tag : " + mQuarter3Tag + ", rt = " + mQuarter3Time);
				BKLog.w"finish tag : " + mEndTag);*/
			} catch(Exception e) {
				e.printStackTrace();
				mIsParsingProblemOccured = true;
			}
		} else {
			mIsParsingProblemOccured = true;
		}
	}
	
	// 20141216 파싱시 문제가 발생한 경우 체크를 위한 메소드 추가
	public boolean isParsingProblemOccured() {
		return mIsParsingProblemOccured;
	}
	
	public String getStartTag() {
		return mStartTag;
	}
	
	public String getQuarterTag(int quarter) {
		String result = null;
		switch(quarter) {
			case 1:
				result = mQuarterTag;
				break;
				
			case 2:
				result = mHalfTag;
				break;
				
			case 3:
				result = mQuarter3Tag;
				break;
		}
		
		return result;
	}
	
	public int getQuarterTime(int quarter) {
		int result = -1;
		switch(quarter) {
			case 1:
				result = mQuarterTime;
				break;
				
			case 2:
				result = mHalfTime;
				break;
				
			case 3:
				result = mQuarter3Time;
				break;
		}
		
		return result;
	}
	
	public String getEndTag() {
		return mEndTag;
	}
	
	public String getSkipTag() {
		return mSkipTag;
	}
	
	public String getChargeTag() {
		return mChargeTag;
	}
	
	public int getChargeTime() {
		return mChargeTime;
	}
	
	private class AttributeObject {
		private String rtValue;
		private String eventValue;
		
		
		public void setRtValue(String value) {
			rtValue = value;
		}
		
		public String getRtValue() {
			return rtValue;
		}
		
		public void setEventValue(String value) {
			eventValue = value;
		}
		
		public String getEventValue() {
			return eventValue;
		}
		
		
	}
}
